package com.englisheveryday.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.englisheveryday.dao.ChangeListDAO;
import com.englisheveryday.dao.ArticleDAO;
import com.englisheveryday.response.ArticleResponse;
import com.englisheveryday.util.UUIDGenerator;
import com.englisheveryday.vo.ArticleVO;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-9-16 上午11:00:11
 * @version 1.1
 */
@Service
public class ArticleService {

	private ArticleDAO articleDAO;
	private ChangeListDAO changeListDAO;
	
	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
		articleDAO = new ArticleDAO(jdbcTemplate);
		changeListDAO = new ChangeListDAO(jdbcTemplate);
	}
	
	//同步每日数据
	public List<ArticleVO> syncArticle(long lastAnchor,long nextAnchor){
		return articleDAO.sync(lastAnchor,nextAnchor);
	}
	
	//查询每日数据
	public List<ArticleVO> query_article(int page,int count){
		return articleDAO.query_article(page, count);
	}
	
	//保存每日口语
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int add_article(String englishtext,String title,String explain,String category,String audio){
		String id = UUIDGenerator.generate();
		articleDAO.save(id,englishtext, title, explain, category, audio);
		return changeListDAO.save_articlechangelist(id, "A");
	}
	
	//删除每日口语
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int delete_article(String id){
		 articleDAO.delete(id);
		 return	changeListDAO.save_articlechangelist(id, "D");
	}
	
	//更新每日口语
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public int update_article(String id,String englishtext,String title,String explain,String category,String audio){
		 articleDAO.update(id, englishtext, title, explain, category, audio);
		 return	changeListDAO.save_articlechangelist(id, "U");
	}
	
	//根据ID查询
	public ArticleResponse get_article(String id){
		return articleDAO.get(id);
	}
	
}

